package edu.uwm.RobberKnight.View;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.Stack;

import edu.uwm.RobberKnight.Model.Knight;
import edu.uwm.RobberKnight.Model.Tile;

public class GuiTile {
	public final static int TILE_SIZE = 75;
	private Tile _tile;
	private int _x;
	private int _y;

	public GuiTile(Tile tile) {
		_tile = tile;
	}
	
	public void drawTileFace(Graphics g, boolean showKnights) {
		Color tileColor = getTileColor();
		Image img = getTileImage();

		g.setColor(tileColor);
		g.fillRect(_x, _y, TILE_SIZE, TILE_SIZE);
		if(img != null)
			g.drawImage(img, _x, _y, TILE_SIZE, TILE_SIZE, null);
		g.setColor(Color.white);
		g.drawRect(_x, _y, TILE_SIZE, TILE_SIZE);
		g.setColor(Color.black);
		g.drawString(_tile.getTileType().name(), _x + 2, _y + 20);
		if (showKnights) {
			Stack<Knight> knights = _tile.getKnights();
			if (knights != null) {
				for (int i = 0; i < knights.size(); i++) {
					g.setColor(knights.get(i).getColor());
					g.fillOval((_x + TILE_SIZE/2), (_y + TILE_SIZE/2) - (i*5), 20, 20);
					g.setColor(Color.black);
					g.drawOval((_x + TILE_SIZE/2), (_y + TILE_SIZE/2) - (i*5), 20, 20);
				}
			}
		}
	}
	
	public void drawTileBack(Graphics g) {
		drawTileBack(g, 1);
	}
	
	public void drawTileBack(Graphics g, float scale) {
		int size = (int)(TILE_SIZE * scale);
		g.setColor(_tile.getPlayer().getColor());
		g.fillRect(_x, _y, size, size);
		g.setColor(Color.white);
		g.drawString(""+_tile.getLetter(), _x + (size / 3), _y + (size / 3));
	}
	
	private Color getTileColor() {
		switch(_tile.getTileType()) {
			case CASTLE_FOREST:
				return new Color(139, 117, 0);
			case CASTLE_PLAIN:
				return new Color(255, 99, 71);
			case PLAIN:
				return new Color(205, 133, 63);
			case MOUNTAIN:
				return new Color(131, 139, 139);
			case LAKE:
				return new Color(126, 192, 238);
			case TOWN_FOREST:
				return new Color(0, 100, 0);
			case TOWN_PLAIN:
				return new Color(222, 184, 35);
			case VILLAGE_FOREST:
				return new Color(0, 255, 0);
			case VILLAGE_PLAIN:
				return new Color(227, 207, 87);
			default:
				return Color.yellow;
		}
	}
	
	private Image getTileImage() {
		switch(_tile.getTileType()) {
			//case CASTLE_PLAIN:
			//	return Toolkit.getDefaultToolkit().getImage("src/edu/uwm/RobberKnight/icons/castle.png");
			default:
				return null;
		}
	}
	
	public Tile getTile() {
		return _tile;
	}

	public int getX() {
		return _x;
	}

	public void setX(int x) {
		_x = x;
	}

	public int getY() {
		return _y;
	}

	public void setY(int y) {
		_y = y;
	}
	
	public boolean containsPoint(int x, int y) {
		return (x >= _x && x <= _x + TILE_SIZE) && (y >= _y && y <= _y + TILE_SIZE);
	}
}
